﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Ogame 的摘要说明
/// </summary>
public class Ogame
{
	public Ogame()
	{
		//
		// TODO: 在此处添加构造函数逻辑

		//
	}
    
    public static string FlashGameDBConnstring =System.Configuration.ConfigurationSettings.AppSettings["gamestr"].ToString();

    #region 登录小游戏
    /// <summary>
    /// 登录小游戏
    /// </summary>
    /// <param name="UserId">用户IDX</param>
    /// <param name="NickName">昵称</param>
    /// <param name="GameID">游戏ID</param>
    /// <param name="RegIP">登录IP</param>
    /// <returns>DataRow</returns>
    public DataRow LoginReg(int UserId, string NickName, int GameID, string RegIP)
    {
        SqlParameter[] p ={
                SqlHelper.MakeInParam("@UserId",SqlDbType.Int,4,UserId),
                SqlHelper.MakeInParam("@NickName",SqlDbType.VarChar,20,NickName),
                SqlHelper.MakeInParam("@GameID",SqlDbType.Int,4,GameID),
                SqlHelper.MakeInParam("@RegIP",SqlDbType.VarChar,20,RegIP),
            };

        DataTable dt = null;

        try
        {
            dt = SqlHelper.ExecuteDataTable(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[GameWeb_OGame_LoginReg]", p);
        }
        catch (Exception ex)
        {
           
        }

        if (dt != null && dt.Rows.Count > 0)
        {
            return dt.Rows[0];
        }

        return null;
    }

    #endregion

    #region 修改积分
    /// <summary>
    /// 修改积分
    /// </summary>
    /// <param name="UserId">用户IDX</param>
    /// <param name="gameID">游戏ID</param>
    /// <param name="point">积分</param>
    /// <returns></returns>
    public bool UpdatePoint(int UserId, int gameID, int point,int gameindex)
    {

        int ret = -1;
        SqlParameter[] p ={ 
                SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4,ret),
                SqlHelper.MakeInParam("@userId",SqlDbType.Int,4,UserId),
                SqlHelper.MakeInParam("@gameID",SqlDbType.Int,4,gameID),
                SqlHelper.MakeInParam("@point",SqlDbType.Int,4,point),
		SqlHelper.MakeInParam("@gameindex",SqlDbType.Int,4,gameindex),
            };

        try
        {
            SqlHelper.ExecuteNonQuery(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[GameWeb_OGame_UpdatePoint_c]", p);
            ret = Convert.ToInt32(p[0].Value);
        }
        catch (Exception ex)
        {
           
        }

        return ret == 1;
    }
    #endregion

    #region 排行榜

    public DataTable GetRank(int gameID)
    {

        SqlParameter[] p ={ 
                SqlHelper.MakeInParam("@gameID",SqlDbType.Int,4,gameID)
            };
        DataTable dt = null;
        try
        {
            dt = SqlHelper.ExecuteDataTable(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[GameWeb_OGame_Rank]", p);
        }
        catch (Exception ex)
        {
          //  ApplicationLog.WriteError(ApplicationLog.FormatException(ex, System.DateTime.Now.ToString()));
        }

        return dt;
    }

    #endregion


    #region 检查金额是否超限
    public long GetLoseMoney(int UserId)
    {

        long ret = 0;
        SqlParameter[] p ={ 
                SqlHelper.MakeOutParam("@ret",SqlDbType.BigInt,8,ret),
                SqlHelper.MakeInParam("@UserId",SqlDbType.Int,4,UserId)
            };

        try
        {
            SqlHelper.ExecuteNonQuery(FlashGameDBConnstring, CommandType.StoredProcedure, "[dbo].[GameWeb_Money_BackCheck]", p);
            ret = Convert.ToInt64(p[0].Value);
        }
        catch (Exception ex)
        {
          //  ApplicationLog.WriteError(ApplicationLog.FormatException(ex, System.DateTime.Now.ToString()));
        }

        return ret;
    }
    #endregion
}
